package com.fjwt.gz.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.LiteFeedbackDTO;
import com.fjwt.gz.db.entity.LiteFeedbackEntity;
import com.fjwt.gz.db.vo.LiteFeedbackVO;
import com.fjwt.gz.service.mapper.LiteFeedbackMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 小程序用户信息反馈表 服务实现类
 * </p>
 *
 * @author zhangyangyang
 * @since 2025-05-08
 */
@Service
public class LiteFeedbackService extends ServiceImpl<LiteFeedbackMapper, LiteFeedbackEntity> {

    @Autowired
    private LiteFeedbackMapper liteFeedbackMapper;

    /**
     * 小程序添加反馈
     * @param feedback
     */
    public void addFeedback(LiteFeedbackDTO feedback) {
        if (StringUtils.isEmpty(feedback.getAppId())|| Objects.isNull(feedback.getFansId())) {
            throw new BizException("网络故障，请稍后重试！");
        }
        if (feedback.getFeedbackText() == null || feedback.getFeedbackText().isEmpty()) {
            throw new BizException("问题描述不能为空");
        }
        if (feedback.getFeedbackText().length() > 350) {
            throw new BizException("问题描述不能超过350字");
        }
        LiteFeedbackEntity liteFeedbackEntity = new LiteFeedbackEntity();
        BeanUtil.copyProperties(feedback, liteFeedbackEntity);
        save(liteFeedbackEntity);
    }

    /**
     * pc查询用户反馈列表
     * @param iPage
     * @return
     */
    public IPage<LiteFeedbackVO> queryList(IPage iPage, LiteFeedbackDTO dto) {
        return this.baseMapper.list(iPage,dto);
    }

    /**
     * pc用户反馈回复
     * @param dto
     */
    public void reply(LiteFeedbackDTO dto) {
        LiteFeedbackEntity liteFeedbackEntity = getById(dto.getLiteFeedbackId());
        if (liteFeedbackEntity == null) {
            throw new BizException("反馈不存在");
        }
        liteFeedbackEntity.setFeedbackReply(dto.getFeedbackReply());
        updateById(liteFeedbackEntity);
    }
}
